package bill.study.concurrency.confinement;

import java.util.Random;

public class ThreadLocal1Test {
	static Random random = new Random();
	public static void main(String args[]) {

	// Each thread increments counter
	// Displays variable info
	// And sleeps for the random amount of time
	// Before displaying info again
	// For random number generation
	 
	Runnable runner = new Runnable() {
		public void run() {
			synchronized (ThreadLocal1.class) {
				ThreadLocal1.counter++;
			}
			ThreadLocal1.threadLocal.set(new Integer(random.nextInt(1000)));
			ThreadLocal1.displayValues();
			try {
				Thread.sleep(((Integer) ThreadLocal1.threadLocal.get()).intValue());
				ThreadLocal1.displayValues();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	};

	// Increment counter, access thread local from
	// a different thread, and display the values
	synchronized (ThreadLocal1.class) {
		ThreadLocal1.counter++;
	}
	
	ThreadLocal1.threadLocal.set(new Integer(random.nextInt(1000)));
	ThreadLocal1.displayValues();

	// Here's where the other threads
	// are actually created
	for (int i = 0; i < 5; i++) {
		Thread t = new Thread(runner);
		t.start();
	}
}
}
